<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload" @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="配件名称:">
              <el-input v-model="where.name" clearable placeholder="请输配件名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:bom:index')" class="ele-btn-icon" icon="el-icon-search" type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where" height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button v-if="permission.includes('sys:bom:add')" class="ele-btn-icon" icon="el-icon-plus" size="small" type="primary" @click="openEdit(null)">
            添加
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link v-if="permission.includes('sys:bom:edit')" :underline="false" icon="el-icon-edit" type="primary" @click="openEdit(row)">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此配件吗？" @confirm="remove(row)">
            <el-link v-show="permission.includes('sys:bom:delete')" slot="reference" :underline="false" icon="el-icon-delete" type="danger">
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <EditBom ref="BomEdit" :data="current" :visible.sync="showEdit" @addBom="addBom" @editBom="editBom" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import EditBom from "./components/bom-edit.vue"

export default {
  name: 'SystemLevel',
  components: {
    EditBom
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/boms',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'name',
          label: '配件名称',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'specification',
          label: '规格',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'model',
          label: '型号',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'uom',
          label: '计量单位',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    // 新增
    addBom(value) {
      this.$http.post('/boms', value).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.BomEdit.updateVisible(false);
          this.reload();
        }else {
          this.$message.error(res.data.msg);
          this.$refs.BomEdit.updateVisible(false);
          this.reload();
        }
        this.$refs.BomEdit.loading = false;
        this.$refs.BomEdit.$refs.form.resetFields();
      }).catch(e => {
        this.$refs.BomEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    // 编辑
    editBom(value, id) {
      this.$http.put('/boms/' + id, value).then(res => {
        this.$refs.BomEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.BomEdit.updateVisible(false);
          this.reload();
        }else {
          this.$message.error(res.data.msg);
          this.$refs.BomEdit.updateVisible(false);
        }
      }).catch(e => {
        this.$refs.BomEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.delete('/boms/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped></style>
